package com.lh.recallbootrabbitmq.manage;

import com.lh.recallbootrabbitmq.exception.RabbitMQExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author lh
 * @Description: 绑定管理
 * @date 2021/8/24 17:06
 */
@Component
public class BindManage extends AbstractManage {
	private final static Logger logger = LoggerFactory.getLogger(BindManage.class);

	@Autowired
	RabbitAdmin rabbitAdmin;

	/**
	 * 队列与交换机进行绑定
	 *
	 * @param queueName    队列名称
	 * @param exchangeName 交换机名称
	 * @param routingKey   路由键
	 * @return
	 */
	public boolean queueBind(String queueName, String exchangeName, String routingKey) {
		return this.bing(queueName, Binding.DestinationType.QUEUE, exchangeName, routingKey, null);
	}

	/**
	 * 交换机和交换机进行绑定
	 *
	 * @param destExchangeName 目标交换机名称
	 * @param exchangeName     交换机名称
	 * @param routingKey       路由键
	 * @return
	 */
	public boolean exchangeBind(String destExchangeName, String exchangeName, String routingKey) {
		return this.bing(destExchangeName, Binding.DestinationType.EXCHANGE, exchangeName, routingKey, null);
	}

	/**
	 * bind绑定
	 *
	 * @param destName     目标名称（可以是队列 也可以是交换机）
	 * @param type         绑定的类型 交换机 / 队列
	 * @param exchangeName 交换机的名称
	 * @param routingKey   路由键
	 * @param map          结构参数
	 * @return
	 */
	private boolean bing(String destName, Binding.DestinationType type, String exchangeName, String routingKey, Map<String, Object> map) {
//		logger.info("bind parameter is destName: " + destName + ", type: " + type.name()
//				+ ", exchangeName: " + exchangeName + ", routingKey: " + routingKey + ", map: " + map.toString());

		Binding binding = new Binding(destName, Binding.DestinationType.QUEUE, exchangeName, routingKey, map);
		try {
			this.rabbitAdmin.declareBinding(binding);
		} catch (Exception e) {
			if (logger.isDebugEnabled()) {
				RabbitMQExceptionUtils.throwRabbitMQException(e.getMessage());
			}
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}

	@Override
	public void DefinedValidate(Object object) {

	}
}
